package gui;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.URL;

import javax.swing.*;

import client.libmessenger.*;


public class MessageFrame extends JFrame implements ComponentListener
{
	// data structures
	private LibMessenger libMessage;
	private String destination, username;
	public String html, head, body, paragraph, style;
	
	// interface elements
	private Container container;
	private GridBagLayout gridBagLayout;
	private GridBagConstraints constraints;
	
	private JLabel lblName, lblHappy, lblSad, lblBigSmile, lblCrying, lblAngry, lblPuke, lblDespair, lblSleepy, lblEmbarrassed, lblSurprise, lblThinking, lblRedLip, lblWinking;
	private JButton btnSend, btnFile, btnGame, btnBlock;
	public JTextPane messagePane, communicationPane;	 
	private JScrollPane messageScroller, communicationScroller;
	private JSeparator separator1, separator2;
	private JPanel buttonPanel, topPanel, centerPanel, mainPanel, bottomPanel;
	
	public MessageFrame(LibMessenger lm, String destination, String username)
	{
		super();
		this.libMessage = lm;
		this.username = username;
		this.destination = destination;
		this.setTitle("MSN Clone ~ Chatting with " + destination);
				
		html = "</*+html>";
		head = "</*+head>";
		body = "</*+body>";
		paragraph = "</*+p>*+";
		style = "style=\"margin-top: 0\"*+>";
		
		// set up the UI
		container = getContentPane();
		container.setLayout(new BorderLayout(5,5));		
		gridBagLayout = new GridBagLayout();
		constraints = new GridBagConstraints();
		
		lblName = new JLabel("To: " + destination);
		lblHappy = new JLabel(new ImageIcon("./Images/Happy.png"));
        lblSad = new JLabel(new ImageIcon("./Images/Sad.png"));
        lblBigSmile = new javax.swing.JLabel(new ImageIcon("./Images/BigSmile.png"));
        lblCrying = new JLabel(new ImageIcon("./Images/Crying.png"));
        lblAngry = new JLabel(new ImageIcon("./Images/Angry.png"));
        lblPuke = new JLabel(new ImageIcon("./Images/Puke.png"));
        lblDespair = new JLabel(new ImageIcon("./Images/Disappear.png"));
        lblSleepy = new JLabel(new ImageIcon("./Images/Sleepy.png"));
        lblEmbarrassed = new JLabel(new ImageIcon("./Images/Embrassed.png"));
        lblSurprise = new JLabel(new ImageIcon("./Images/Surprise.png"));
        lblThinking = new JLabel(new ImageIcon("./Images/Thinking.png"));
        lblRedLip = new JLabel(new ImageIcon("./Images/RedLip.png"));
        lblWinking = new JLabel(new ImageIcon("./Images/Winking.png"));
        
        lblHappy.setToolTipText("Happy");
        lblSad.setToolTipText("Sad");
        lblBigSmile.setToolTipText("Big Smile");
        lblCrying.setToolTipText("Crying");
        lblAngry.setToolTipText("Angry");
        lblPuke.setToolTipText("Puke");
        lblDespair.setToolTipText("Desppointed");
        lblSleepy.setToolTipText("Sleepy");
        lblEmbarrassed.setToolTipText("Embarrassed");
        lblSurprise.setToolTipText("Surprise");
        lblThinking.setToolTipText("Thinking");
        lblRedLip.setToolTipText("Red Lip");
        lblWinking.setToolTipText("Winking");
        lblHappy.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                lblHappyMouseClicked(evt);
            }
        });
		
        lblSad.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                lblSadMouseClicked(evt);
            }
        });
        lblBigSmile.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblBigSmileMouseClicked(evt);
            }
        });
        lblCrying.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblCryingMouseClicked(evt);
            }
        });
        lblAngry.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblAngryMouseClicked(evt);
            }
        });
        lblPuke.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblPukeMouseClicked(evt);
            }
        });        
        lblDespair.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblDespairMouseClicked(evt);
            }
        });
        lblSleepy.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblSleepyMouseClicked(evt);
            }
        });
        lblEmbarrassed.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblEmbarrassedMouseClicked(evt);
            }
        });
        lblSurprise.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblSurpriseMouseClicked(evt);
            }
        });
        lblThinking.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblThinkingMouseClicked(evt);
            }
        });
        lblRedLip.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblRedLipMouseClicked(evt);
            }
        });
        lblWinking.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
            	lblWinkingMouseClicked(evt);
            }
        });
        
        btnSend = new JButton("Send Message");
        btnFile = new JButton("Send File");
        btnGame = new JButton("Play a Game");
        btnBlock = new JButton("Block User");
        
        btnBlock.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent evt) {
             btnBlockMouseClicked(evt);
         }
     });
        
        btnGame.setAlignmentY(JButton.CENTER);
		btnGame.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                btnGameMouseClicked(evt);
            }
        });
		btnFile.setAlignmentY(JButton.CENTER);
		btnFile.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                btnFileMouseClicked(evt);
            }
        });
		
		btnSend.setAlignmentY(JButton.CENTER);
		btnSend.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                btnSendMouseClicked(evt);
            }
        });		
		
        messagePane = new JTextPane();
        communicationPane = new JTextPane();
        
        messagePane.setContentType("text/html");
        messagePane.setDoubleBuffered(true);
        communicationPane.setForeground(Color.BLACK);
        communicationPane.setContentType("text/html");
        communicationPane.setEditable(false);
        communicationPane.setDoubleBuffered(true);
                               
        messageScroller = new JScrollPane(messagePane);
        messageScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        communicationScroller = new JScrollPane(communicationPane);        
        communicationScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        separator1 = new JSeparator();
        separator1.setAlignmentX(JSeparator.CENTER_ALIGNMENT);
        separator2 = new JSeparator();
        separator2.setAlignmentX(JSeparator.CENTER_ALIGNMENT);
        
        topPanel = new JPanel();
		topPanel.setLayout(new BorderLayout(5, 5));
        centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout( 5, 5));
        buttonPanel = new JPanel();
        buttonPanel.setLayout(gridBagLayout);
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(2, 1, 5, 5));        
        bottomPanel = new JPanel();
		bottomPanel.setLayout(new GridLayout(1, 13, 5, 5));
 
		// add the labels	
		lblName.setFont(new Font("Arial", Font.BOLD, 12));
		
		topPanel.add(lblName, BorderLayout.NORTH);
		topPanel.add(communicationScroller, BorderLayout.CENTER);
		topPanel.add(separator1, BorderLayout.SOUTH);
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
		addComponent(gridBagLayout, buttonPanel, btnGame, 0, 0, 2, 1);
		addComponent(gridBagLayout, buttonPanel, btnFile, 1, 0, 2, 1);
		addComponent(gridBagLayout, buttonPanel, btnSend, 2, 0, 2, 1);
		addComponent(gridBagLayout, buttonPanel, btnBlock, 3, 0, 2, 1);
	
        centerPanel.add(messageScroller, BorderLayout.CENTER);
        centerPanel.add(buttonPanel, BorderLayout.EAST);
        centerPanel.add(separator2, BorderLayout.SOUTH);		
   		
        mainPanel.add(topPanel);
        mainPanel.add(centerPanel);
        
		bottomPanel.add(lblHappy);
		bottomPanel.add(lblSad);
		bottomPanel.add(lblBigSmile);
		bottomPanel.add(lblCrying);;
		bottomPanel.add(lblAngry);
		bottomPanel.add(lblPuke);
		bottomPanel.add(lblDespair);
		bottomPanel.add(lblSleepy);;
		bottomPanel.add(lblEmbarrassed);
		bottomPanel.add(lblSurprise);
		bottomPanel.add(lblThinking);
		bottomPanel.add(lblRedLip);
		bottomPanel.add(lblWinking);

		container.add(mainPanel, BorderLayout.CENTER);
		container.add(bottomPanel, BorderLayout.SOUTH);		
		addComponentListener(this);
		
		this.pack();
		this.setSize(500, 450);
		//this.setSize((int) getSize().width*1.2, (int) getSize().height*1.2);
        
	}
    public void componentMoved(ComponentEvent e) {}
    public void componentShown(ComponentEvent e) {}
    public void componentHidden(ComponentEvent e) {}
	public void componentResized(ComponentEvent e) {
        int width = getWidth();
        int height = getHeight();
        int minWidth = 450, minHeight = 400;
        
        boolean resize = false;
        if (width < minWidth) {
             resize = true;
             width = minWidth;
        }    
        if (height < minHeight) {
        	resize = true;
            height = minHeight;
        }
        if (resize)
            setSize(width, height);
     }
	private void addComponent(GridBagLayout lay, JPanel pan,  Component component, int row, int column, int width, int height){
		constraints.gridx = column;
		constraints.gridy = row;
		constraints.gridwidth = width;
		constraints.gridheight = height;
		lay.setConstraints(component, constraints);
		pan.add(component);
	}
	
    private void btnFileMouseClicked(MouseEvent evt) {
   	 MessengerWindow.instance.uploadFileToUser(this.destination);            
    }

    private void btnGameMouseClicked(MouseEvent evt) {
		TicTacToeWindow TTT = new TicTacToeWindow(this.username, this.destination);
	   	TTT.setVisible(true);
	   	TTT.requestFocus();
	    		
	   	MessengerWindow.instance.TTTwindows.add(TTT);
	   	libMessage.initiateGame(this.destination, "TTT");    	
        
    }

    private void btnSendMouseClicked(MouseEvent evt) {
    	String conversation;
    	String message = messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").replaceAll(paragraph,"").replaceAll(style, "").trim();
    	System.out.println("MESSAGE IS " + message + "   end");
        if (message.compareTo("") == 0)
        {
            JOptionPane.showMessageDialog(null, "Empty message! Message cannot be sent", "Warning", JOptionPane.INFORMATION_MESSAGE);
        } // IF
        else 
        {
	        	//message = message.replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim();
	         conversation = communicationPane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").replaceAll(paragraph,"").replaceAll(style, "").trim();
	         communicationPane.setText(conversation + "<br>" + username + " says: " + message);
	         messagePane.setText("");
	            
	         if(!libMessage.sendMessage(this.username, this.destination, message.trim()))
	        	 communicationPane.setText(conversation + "<br>" + "Message delivery failed to user: "+this.destination+"<br>Reason: "+libMessage.lastError); 
	        			 
	        			 //this.username + " says: " + message);
	         //System.out.println(messagePane.getText());
        } // ELSE
        
    }
/*	public static void main(String[] args)
	{
		new MessageFrame(new LibMessenger(), "destination", "Agnes").setVisible(true);
	}*/
   
    private void btnBlockMouseClicked(MouseEvent evt)
    {
   	 if(this.libMessage.blockUser(this.destination))
   	 {
   		 MessengerWindow.instance.getContactByName(this.destination).blocked = true;   		 
   		 this.setVisible(false);
   		 MessengerWindow.instance.reloadContactList();
   		 MessengerWindow.instance.reloadTree();   		 
   	 } // IF
   	 else
   		 JOptionPane.showMessageDialog(null, "Block user failed!\n"+this.libMessage.lastError, "Block Failed", JOptionPane.ERROR_MESSAGE);
    }
    
    private void lblHappyMouseClicked(MouseEvent evt){        
    	URL urlLocation = this.getClass().getResource("../Images/Happy.png");        	
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    	System.out.println(messagePane.getText());
    }
	
    private void lblSadMouseClicked(MouseEvent evt){    	
    	URL urlLocation = this.getClass().getResource("../Images/Sad.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");    	
    }
    
    private void lblBigSmileMouseClicked(MouseEvent evt){    	
    	URL urlLocation = this.getClass().getResource("../Images/BigSmile.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    }
    private void lblCryingMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Crying.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">"); 
    }
    private void lblAngryMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Angry.png");  
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");    	
    }

    private void lblPukeMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Puke.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");   	
    }

    private void lblDespairMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Disappear.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    }	

    private void lblSleepyMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Sleepy.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    }

    private void lblEmbarrassedMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Embrassed.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    }

    private void lblSurpriseMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Surprise.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    }

    private void lblThinkingMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Thinking.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    }

    private void lblRedLipMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/RedLip.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");        	
    }
    private void lblWinkingMouseClicked(MouseEvent evt){
    	URL urlLocation = this.getClass().getResource("../Images/Winking.png");
    	messagePane.setText( messagePane.getText().replaceAll(html, "").replaceAll(head, "").replaceAll(body,"").trim().replaceAll(paragraph,"").trim().replaceAll(style, "")+  "<img src= file:" + urlLocation.getPath() + ">");
    }

}
